import axios from "axios";
import { startDate, endDate } from "./config.js";

// 查询从本月的工作日
export const queryWorkingDay = () => {
  return new Promise((resolve, reject) => {
    const url = "https://www.iamwawa.cn/home/workingday/ajax";
    const params = {
      start_date: startDate.format("YYYY-MM-DD"),
      end_date: endDate.format("YYYY-MM-DD"),
    };

    axios
      .post(url, params, { headers: { "Content-Type": "application/x-www-form-urlencoded" } })
      .then(({ data: res }) => {
        const { status, data, info } = res;
        const {
          // 平常的周末
          weekend_date_list = [],
          // 法定节假日
          holiday_date_list = [],
          // 法定节假日补班
          tune_date_list = [],
          // 工作日天数
          working_date_count,
        } = data;

        // console.log(data);
        const dayOfMonth = [];
        for (let day = startDate; day.valueOf() <= endDate.valueOf(); day = day.add(1, "day")) {
          dayOfMonth.push(day.format("YYYY-MM-DD"));
        }

        // 需要排除的法定节假日和周末
        const excludeDays = [
          weekend_date_list.map((item) => item.date),
          holiday_date_list.map((item) => item.date),
        ].flat();

        // 工作日
        const workDays = dayOfMonth.filter((day) => !excludeDays.includes(day));

        //   console.log(status,data,info);
        console.log(`本月你需要补充${working_date_count}天日报`);
        console.log(`需要填写的日期:`);
        workDays.forEach((day) => {
          console.log(day);
        });

        console.log(`需要排除的日期:`);
        excludeDays.forEach((day) => {
          console.log(day);
        });

        resolve(workDays);
      });
  });
};
